﻿#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

#include "stdio.h"
#include "gps_rmc.h"
#include "math.h"

char *buff[] = {
"$GPGSA,A,3,12,02,06,,,,,,,,,,17.24,13.56,10.65,1*2B",
"$BDGSA,A,3,213,228,,,,,,,,,,,17.24,13.56,10.65,4 * 30",
"$GPGSV,3,1,11,12,14,321,26,2,32,271,23,6,61,301,20,24,2,277,14 * 4D",
"$GPGSV,3,2,11,199,45,149,,194,38,195,,14,31,172,,193,29,85,* 70",
"$GPGSV,3,3,11,3,17,40,,9,14,120,,4,13,84,* 7D",
"$BDGSV,4,1,13,213,56,260,24,228,59,334,20,210,69,212,,207,53,171,* 65",
"$BDGSV,4,2,13,203,50,186,,214,47,89,,201,38,133,,218,35,302,* 57",
"$BDGSV,4,3,13,204,27,117,,211,21,85,,205,19,248,,227,15,292,* 5E",
"$BDGSV,4,4,13,223,15,208,* 67",
"$GNRMC,023338.000,A,3450.14968,N,11330.05153,E,3.168,276.87,050821,,,A * 45",
"$GNZDA,023338.000,05,08,2021,00,00 * 4D",
"$GNGGA,023339.000,3450.15016,N,11330.04992,E,1,05,13.56,186.5,M,-16.4,M,,* 6D",
"$GPGSA,A,3,12,02,06,,,,,,,,,,17.24,13.56,10.65,1 * 2B",
"$BDGSA,A,3,213,228,,,,,,,,,,,17.24,13.56,10.65,4 * 30",
"$GPGSV,3,1,11,12,14,321,25,2,32,271,22,6,61,301,19,24,2,277,14 * 45",
"$GPGSV,3,2,11,199,45,149,,194,38,195,,14,31,172,,193,29,85,* 70",
"$GPGSV,3,3,11,3,17,40,,9,14,120,,4,13,84,* 7D",
"$BDGSV,4,1,13,213,56,260,23,228,59,334,20,215,73,23,,207,53,171,* 5C",
"$BDGSV,4,2,13,203,50,186,,214,47,89,,201,38,133,,218,35,302,* 57",
"$BDGSV,4,3,13,204,27,117,,211,21,85,,205,19,248,,227,15,292,* 5E",
"$BDGSV,4,4,13,223,15,208,* 67",
"$GNRMC,023339.000,A,3450.15016,N,11330.04992,E,5.176,295.87,050821,,,A * 45",
"$GNZDA,023339.000,05,08,2021,00,00 * 4C"
};

#define GPS_DATE_TO_STRING_FORMAT "%d-%02d-%02d %02d:%02d:%02d"
//int main()
//{
//
//	unsigned int index = 0;
//	printf("111\r\n");
//	for (index = 0; index < sizeof(nmea); ++index)
//	{
//
//		if (nmea_decode(&nmea0183, nmea[index])) ///调用解析函数
//		{
//			printf("LAT:%f,LNG:%f\r\n", nmea0183.gpsData.location.lat, nmea0183.gpsData.location.lng);
//			printf("new_speed:%f\r\n", nmea0183.gpsData.ground_speed);
//			printf("new_course:%f\r\n", nmea0183.gpsData.ground_course);
//			printf("可见卫星颗数:%d\r\n", nmea0183.gpsData.num_sats);
//			printf("GPS定位状态:%d\r\n", nmea0183.gpsData.status);
//		}
//	}
//	return 0;
//}

double getSpeed(double lng_a, double lat_a, double lng_b, double lat_b) {
    double x = pow((lng_b - lng_a), 2);
    double y = pow((lat_b - lat_a), 2);
    double speed = sqrt(x + y) * 111322.2222 * 3600 / 1000 / 1.825;
    return speed;
}
char lat[20];
char lng[20];
char ns;


#define PI                      3.1415926
#define EARTH_RADIUS            6378.137        //地球近似半径

double radian(double d);
double get_distance(double lat1, double lng1, double lat2, double lng2);

// 求弧度
double radian(double d)
{
    return d * PI / 180.0;   //角度1˚ = π / 180
}

//计算距离
double get_distance(double lat1, double lng1, double lat2, double lng2)
{
    double radLat1 = radian(lat1);
    double radLat2 = radian(lat2);
    double a = radLat1 - radLat2;
    double b = radian(lng1) - radian(lng2);

    double dst = 2 * asin((sqrt(pow(sin(a / 2), 2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2), 2))));

    dst = dst * EARTH_RADIUS;
    dst = round(dst * 10000) / 10000;
    return dst;
}


void test_scanf()
{
    char result[128] = { 0 };
    char buf[] = {0x22,0x38,0x36,0x37 ,0x31 ,0x38 ,0x31 ,0x30  ,0x35 ,0x30 ,0x32 ,0x30 ,0x33 ,0x39 ,0x36 ,0x37 ,0x22 ,0x0D ,0x0A };
    int ret;
   ret=sscanf(buf, "\"%s[^\"]", &result);


  

    printf("result %d\r\n",ret);

    printf("%s\r\n", result);


    //printf("%s\r\n", buf);

    //ret = sscanf(buf, "%s", result);


    //printf("result %d\r\n", ret);

    //printf("%s",result);

}




void testspeed()
{
    //double lat1 = 39.90744;
    //double lng1 = 116.41615;//经度,纬度1
    //double lat2 = 39.90744;
    //double lng2 = 116.30746;//经度,纬度2
    

    double lat1 = 34.8341102;
    double lng1 = 113.4765320;//经度,纬度1
    double lat2 = 34.8344841;
    double lng2 = 113.4765320;//经度,纬度2


    //double lat1 = 34.83596128;
    //double lng1 = 113.49227107;//经度,纬度1
    //double lat2 = 34.83596538;
    //double lng2 = 113.49346430;//经度,纬度2
    
    
    double dst = get_distance(lat1, lng1, lat2, lng2);

    printf("dst = %0.3fkm\n", dst);  //dst = 9.281km
    printf("speed = %0.3fkm\n", dst / 3 * 3600);  //dst = 9.281km

   
}


int _main(int args, char** argv)
{

   // test_scanf();

    testspeed();

    return 0;

    struct gps_info info;
    char buff[] = "+MGPSGETLOC: 3450.13079,N,11330.09074,EOK";
    char* buf=buff;
    info.coord.source.latitude.value = 0;
    info.coord.source.longitude.value = 0;

    
    info.coord.source.latitude.value = 0;
    info.coord.source.longitude.value = 0;
    sscanf(buf, "+MGPSGETLOC: %[^,],%*[^,],%[^,],%*s", &info.coord.source.latitude.string, &info.coord.source.longitude.string);

    gps_coord_handle(&info);

    sscanf(buf, "*[A-Z]", &info.coord.source.latitude.string, &info.coord.source.longitude.string);

    printf("Location coord: %s, %s,%c\r\n", info.coord.location.latitude.string, info.coord.location.longitude.string, info.NS);

   

 /*   char* buff[] = {
        "$GNRMC,230503.000,A,2468.94009,N,11878.72983,E,1.86,256.22,060720,,,A*60"
        "$GNRMC,154123.000,V,,,,,,,060720,,,M*53",
        "$GPRMC,023011.00,V,,,,,,,080720,,,N*71",
        "$GNRMC,,V,,,,,,,,,,M*4E"
    };*/

   //   char* buff2[] = {
   //    "$GNRMC,023339.000,A,3450.15016,N,11330.04992,E,5.176,295.87,050821,,,A * 45"
   //    "$GNRMC,154123.000,V,,,,,,,060720,,,M*53",
   //    "$GPRMC,023011.00,V,,,,,,,080720,,,N*71",
   //    "$GNRMC,,V,,,,,,,,,,M*4E"
   //};
   //
   // uint8_t len = sizeof(buff) / sizeof(uint8_t*), i;
   // printf("\r\nstruct size: %ld\tlen: %d\r\n\r\n", sizeof(struct gps_info), len);
   // for (i = 0; i < len; i++) {
   //
   //     memset(&info, 0, sizeof(struct gps_info));
   //     if (strstr((const char*)buff[i], "RMC")) {
   //         gps_rmc_parse(&info, buff[i]);
   //         gps_print_info(&info);
   //     }
   //     printf("---------------------------------------------------\r\n");
   // }
   // printf("\r\n");
    return 0;
}